﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace _4SquareLite
{
    public partial class ucVenueListItem : UserControl
    {
        string _strVenueID;
        string _strVenueName;
        string _strIconURL;
        string _strCategory;
        string _strLat;
        string _strLon;

        public ucVenueListItem(XmlNode xmlVenue)
        {
            InitializeComponent();

            LoadData(xmlVenue, "");
        }

        public ucVenueListItem(XmlNode xmlVenue, string strCreated)
        {
            InitializeComponent();

            LoadData(xmlVenue, strCreated);
        }

        private void LoadData(XmlNode xmlVenue, string strCreated)
        {
            //id
            _strVenueID = Program.getInnerText(xmlVenue, "id");

            //name
            _strVenueName = Program.getInnerText(xmlVenue, "name");
            lblName.Text = _strVenueName;

            //herenow
            if (String.IsNullOrEmpty(strCreated))
            {
                string strHereNow = Program.getInnerText(xmlVenue, "stats/herenow");
                if (!String.IsNullOrEmpty(strHereNow))
                {
                    int iHereNow = int.Parse(strHereNow);
                    if (iHereNow == 0)
                    {
                        lblHereNow.Text = "";
                    }
                    else if (iHereNow == 1)
                    {
                        lblHereNow.Text = "1 user here now";
                    }
                    else
                    {
                        lblHereNow.Text = iHereNow + " users here now";
                    }
                }
                else
                {
                    lblHereNow.Text = "";
                }
            }
            else
            {
                lblHereNow.Text = strCreated;
                lblHereNow.TextAlign = ContentAlignment.TopRight;
            }


            //primarycategory
            XmlNode xmlCategory = xmlVenue.SelectSingleNode("primarycategory");
            lblCategory.Text = "";
            if (xmlCategory != null)
            {
                //nodename
                _strCategory = Program.getInnerText(xmlCategory, "nodename");
                lblCategory.Text = _strCategory;

                //iconurl
                _strIconURL = Program.getInnerText(xmlCategory, "iconurl");
                pbIcon.Image = Program.getImageFromURL(_strIconURL);
            }

            lblAddress.Text = Program.buildAddress(xmlVenue);

            _strLat = Program.getInnerText(xmlVenue, "geolat");
            _strLon = Program.getInnerText(xmlVenue, "geolong");

            //distance
            string strDistance = Program.getInnerText(xmlVenue, "distance");
            if (!String.IsNullOrEmpty(strDistance))
            {
                strDistance = Program.convertDistanceToKM(strDistance);
                strDistance = lblHereNow.Text + " " + strDistance + " away";
                lblHereNow.Text = strDistance.Trim();
            }
        }

        private void btnCheckin_Click(object sender, EventArgs e)
        {
            frmCheckin oForm = new frmCheckin(_strVenueID, _strVenueName, _strIconURL, _strCategory, _strLat, _strLon);
            oForm.Show();

            var oObject = this.Parent;
            while (!oObject.ToString().Contains("frm"))
            {
                oObject = oObject.Parent;
            }

            oObject.Hide();
        }

        private void ucVenueListItem_Click(object sender, EventArgs e)
        {
            frmVenueDetails oForm = new frmVenueDetails(_strVenueID);
            oForm.Show();

            var oObject = this.Parent;
            while (!oObject.ToString().Contains("frm"))
            {
                oObject = oObject.Parent;
            }

            oObject.Hide();
        }
    }
}
